﻿using Engine.Audio;
using Engine.Media;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentPlayerSystem : Component, IUpdateable
    {
        public ComponentPlayer m_componentPlayer;

        public SubsystemTime m_subsystemTime;

        public SubsystemTerrain m_subsystemTerrain;

        public double m_lastrunTime;

        private bool m_running = false;

        private bool m_swimming = false;

        public bool flag = false;

        public float m_oldwalkspeed;

        public static StreamingSound BgmMusic;

        public static string lastmusictype = "9766";

        public static string[] MusicName = new string[0];

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public int[] DrawOrders => new int[1] { 1 };

        public bool Running => m_running;

        public bool Swimming => m_swimming;

        public object PlayerInfo { get; internal set; }

        public void Update(float dt)
        {
            if (m_componentPlayer.ComponentInput.PlayerInput.Move.Z > 0f && m_componentPlayer.ComponentVitalStats.Stamina > 0.2f)
            {
                if (m_subsystemTime.GameTime - m_lastrunTime < 0.16)
                {
                    m_running = true;
                }

                flag = true;
            }
            else if (m_subsystemTime.GameTime - m_lastrunTime > 0.16 && flag)
            {
                m_lastrunTime = m_subsystemTime.GameTime;
                m_running = false;
                flag = false;
            }
            else
            {
                flag = false;
            }

            if (m_running)
            {
                for (int i = 0; i < 5; i++)
                {
                    m_componentPlayer.ComponentVitalStats.UpdateStamina();
                }

                m_componentPlayer.ComponentVitalStats.UpdateFood();
                m_componentPlayer.ComponentLocomotion.WalkSpeed = m_oldwalkspeed * 1.5f;
            }
            else
            {
                m_componentPlayer.ComponentLocomotion.WalkSpeed = m_oldwalkspeed;
            }

            if (m_componentPlayer.ComponentBody.ImmersionFactor > 0.4f)
            {
                m_swimming = m_running;
            }
            else
            {
                m_swimming = false;
            }

            if (m_componentPlayer.ComponentBody.Position.Y > 50f)
            {
                if (lastmusictype != "far")
                {
                    if (BgmMusic != null)
                    {
                        BgmMusic.Stop();
                    }

                    BgmMusic = new StreamingSound(ContentManager.Get<StreamingSource>(MusicName[0]).Duplicate(), MusicManager.Volume, 1f, 0f, isLooped: false, disposeOnStop: false, 1f);
                    lastmusictype = "far";
                    BgmMusic.Play();
                }
            }
            else if (lastmusictype != "under")
            {
                if (BgmMusic != null)
                {
                    BgmMusic.Stop();
                }

                BgmMusic = new StreamingSound(ContentManager.Get<StreamingSource>(MusicName[0]).Duplicate(), MusicManager.Volume, 1f, 0f, isLooped: false, disposeOnStop: false, 1f);
                lastmusictype = "under";
                BgmMusic.Play();
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            base.Load(valuesDictionary, idToEntityMap);
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_oldwalkspeed = m_componentPlayer.ComponentLocomotion.WalkSpeed;
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            base.Save(valuesDictionary, entityToIdMap);
        }

        public override void Dispose()
        {
            BgmMusic.Stop();
        }
    }
}